//
//  THSystemAuthManager.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 HZMengLiao. All rights reserved.
//
#import "THSystemAuthManager.h"

#import <UserNotifications/UserNotifications.h>

@interface THSystemAuthManager()

@property(nonatomic, strong)CLLocationManager *manager;

@end

@implementation THSystemAuthManager

+ (THSystemAuthManager *)sharedManager
{
    static THSystemAuthManager *singleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[THSystemAuthManager alloc]init];
        singleton.manager =  [[CLLocationManager alloc] init];
        
    });
    return singleton;
}

#pragma mark 检查权限

/**
 *  检查摄像头视频权限
 *  @param complete 是否开启的操作
 */
- (void)kCheckSystemCameraAuthComplete:(THCheckSystemCameraAuthComplete)complete
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    BOOL flag = (authStatus == AVAuthorizationStatusAuthorized);
    if (complete)
    {
//        complete(flag,authStatus);
        KExecuteBlockOnMain(complete,flag,authStatus);
    }
}
/**
 *  检查麦克风音频权限
 *  @param complete 是否开启的操作
 */
- (void)kCheckSystemAudioAuthComplete:(THCheckSystemAudioAuthComplete)complete
{
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    BOOL flag = (authStatus == AVAuthorizationStatusAuthorized);
    if (complete)
    {
//        complete(flag,authStatus);
        KExecuteBlockOnMain(complete,flag,authStatus);
    }
}
/**
 *  检查相册权限
 *  @param complete 是否开启的操作
 */
- (void)kCheckSystemPhotoAlbumAuthComplete:(THCheckSystemPhotoAlbumAuthComplete)complete
{
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
    BOOL flag = (authStatus == PHAuthorizationStatusAuthorized);
    if (complete)
    {
//        complete(flag,authStatus);
        KExecuteBlockOnMain(complete,flag,authStatus);
    }
}
/**
 *  检查通讯录权限
 *  @param complete 是否开启的操作
 */
//- (void)kCheckSystemAddressBookAuthComplete:(THCheckSystemAddressBookAuthComplete)complete
//{
//    ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
//    BOOL flag = authStatus == kABAuthorizationStatusAuthorized;
//    if (complete)
//    {
////        complete(flag,authStatus);
//        KExecuteBlockOnMain(complete,flag,authStatus);
//    }
//}
/**
 *  检查通知权限
 *  @param complete 是否开启的操作
 */
- (void)kCheckSystemNotificationAuthComplete:(THCheckSystemNotificationAuthComplete)complete
{
    UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    UIUserNotificationType type = UIUserNotificationTypeNone;
    BOOL flag = NO;
    if (settings != nil)
    {
        type = settings.types;
        flag = type != UIUserNotificationTypeNone;
    }
    if (complete)
    {
//        complete(flag,settings.types);
        KExecuteBlockOnMain(complete,flag,type);
    }
}
///**
// *  检查日历权限
// *  @param complete 是否开启的操作
// */
//- (void)kCheckSystemCalendarAuthComplete:(THCheckSystemCalendarAuthComplete)complete
//{
//    EKAuthorizationStatus authStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
//    BOOL flag = authStatus == EKAuthorizationStatusAuthorized;
//    if (complete)
//    {
////        complete(flag,authStatus);
//        KExecuteBlockOnMain(complete,flag,authStatus);
//    }
//}
///**
// *  检查备忘录权限
// *  @param complete 是否开启的操作
// */
//- (void)kCheckSystemReminderAuthComplete:(THCheckSystemReminderAuthComplete)complete
//{
//    EKAuthorizationStatus authStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
//    BOOL flag = authStatus == EKAuthorizationStatusAuthorized;
//    if (complete)
//    {
////        complete(flag,authStatus);
//        KExecuteBlockOnMain(complete,flag,authStatus);
//    }
//}
//
/**
 *  检查定位权限
 *  @param complete 是否开启的操作
 */
- (void)kCheckSystemLocationAuthComplete:(THCheckSystemLocationAuthComplete)complete
{
    CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    BOOL flag = (authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse);
    if (complete)
    {
//        complete(flag,authStatus);
        KExecuteBlockOnMain(complete,flag,authStatus);
    }
}
//
///**
// *  检查蓝牙权限
// *  @param complete 是否开启的操作
// */
//- (void)kCheckSystemBluetoothAuthComplete:(THCheckSystemBluetoothAuthComplete)complete
//{
//    CBPeripheralManagerAuthorizationStatus authStatus = [CBPeripheralManager authorizationStatus];
//    BOOL flag = (authStatus == CBPeripheralManagerAuthorizationStatusAuthorized);
//    if (complete)
//    {
////        complete(flag,authStatus);
//        KExecuteBlockOnMain(complete,flag,authStatus);
//    }
//}

#pragma mark 请求权限
/**
 *  请求摄像头视频权限
 *  @param complete 是否开启的操作
 */
- (void)kRequestSystemCameraAuthComplete:(THRequestSystemCameraAuthComplete)complete
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        if (complete)
        {
//            complete(granted);
            KExecuteBlockOnMain(complete,granted);
        }
    }];
}
/**
 *  请求麦克风音频权限
 *  @param complete 是否开启的操作
 */
- (void)kRequestSystemAudioAuthComplete:(THRequestSystemAudioAuthComplete)complete
{
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        if (complete)
        {
//            complete(granted);
            KExecuteBlockOnMain(complete,granted);
        }
    }];
}

/**
 *  请求相册权限
 *  @param complete 是否开启的操作
 */
- (void)kRequestSystemPhotoAlbumAuthComplete:(THRequestSystemPhotoAlbumAuthComplete)complete
{
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        BOOL flag = status == PHAuthorizationStatusAuthorized;
        if (complete)
        {
//            complete(flag,status);
            KExecuteBlockOnMain(complete,flag,status);
        }
    }];
}
/**
 *  请求通讯录权限
 *  @param complete 是否开启的操作
 */
//- (void)kRequestSystemAddressBookAuthComplete:(THRequestSystemAddressBookAuthComplete)complete
//{
//    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
//    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
//                                             {
//                                                 if (complete)
//                                                 {
////                                                     complete(granted,error);
//                                                     KExecuteBlockOnMain(complete,granted,error);
//                                                 }
//                                             });
//}
/**
 *  请求通知权限
 *
 */
- (void)kRequestSystemNotificationAuth
{
    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
///**
// *  请求日历权限
// *  @param complete 是否开启的操作
// */
//- (void)kRequestSystemCalendarAuthComplete:(THRequestSystemCalendarAuthComplete)complete
//{
//    EKEventStore *store = [[EKEventStore alloc]init];
//    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error)
//     {
//         if (complete)
//         {
////             complete(granted,error);
//             KExecuteBlockOnMain(complete,granted,error);
//         }
//     }];
//}
///**
// *  请求备忘录权限
// *  @param complete 是否开启的操作
// */
//- (void)kRequestSystemReminderAuthComplete:(THRequestSystemReminderAuthComplete)complete
//{
//    EKEventStore *store = [[EKEventStore alloc]init];
//    [store requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError * _Nullable error)
//     {
//         if (complete)
//         {
////             complete(granted,error);
//             KExecuteBlockOnMain(complete,granted,error);
//         }
//     }];
//}
//
/**
 *  请求定位权限
 *  @param type 是否开启的操作
 */
- (void)kRequestSystemLocationAuthWithType:(THRequestSystemLocationAuthType)type
{
    switch (type)
    {
        case THRequestSystemLocationAuthAlways:
            [self.manager requestAlwaysAuthorization];
            break;
        case THRequestSystemLocationAuthWhenInUse:
            [self.manager requestWhenInUseAuthorization];
            break;
        default:
            break;
    }
    
}
@end
